package com.asman.vr.answer;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.asman.vr.VRWebView;
import com.asman.vr.dto.SchemeInfoDetailData;
import com.google.gson.Gson;

import java.util.Map;
import java.util.Objects;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

import static io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import static io.flutter.plugin.common.MethodChannel.Result;

public class FlutterAnswerView implements PlatformView, MethodCallHandler  {

    private final AnswerView answerView;
    private final MethodChannel methodChannel;

    FlutterAnswerView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {
        methodChannel = new MethodChannel(messenger, "plugins.com.asman/answer_" + id);
        methodChannel.setMethodCallHandler(this);

        int schemeId = (int) params.get("schemeId");
        int roomId = (int) params.get("roomId");
        String headUrl = (String) params.get("headUrl");
        String name = (String) params.get("name");
        String userId = Objects.requireNonNull(params.get("userId")).toString();
        Long accountId = (Long) params.get("accountId");
        int vrType = (int) params.get("vrType");
        String groupId = (String) params.get("groupId");

        String schemeInfoDetailDataJson = (String) params.get("schemeInfoDetailData");
        Gson gson = new Gson();
        SchemeInfoDetailData schemeInfoDetail =
                gson.fromJson(schemeInfoDetailDataJson, SchemeInfoDetailData.class);

        answerView = new AnswerView(
                context, methodChannel,
                schemeId, roomId, headUrl, name,
                userId, accountId, vrType, groupId,
                schemeInfoDetail
        );
    }

    @Override
    public View getView() {
        return answerView;
    }

    @Override
    public void onMethodCall(MethodCall methodCall, Result result) {
        switch (methodCall.method) {
            case "validCallbackNameFirst":
                this.answerView.getCheckRoomCallback().validCallbackNameFirst();
                break;
            case "invalidCallbackNameFirst":
                this.answerView.getCheckRoomCallback().invalidCallbackNameFirst();
                break;
            case "errorCallbackNameFirst":
                this.answerView.getCheckRoomCallback().errorCallbackNameFirst();
                break;

            case "validCallbackName3Seconds":
                this.answerView.getCheckRoomCallback3Seconds().validCallbackName3Seconds();
                break;
            case "invalidCallbackName3Seconds":
                this.answerView.getCheckRoomCallback3Seconds().invalidCallbackName3Seconds();
                break;
            case "errorCallbackName3Seconds":
                this.answerView.getCheckRoomCallback3Seconds().errorCallbackName3Seconds();
                break;

            case "validCallbackNameAccept":
                this.answerView.getCheckRoomCallbackAccept().validCallbackNameAccept();
                break;
            case "invalidCallbackNameAccept":
                this.answerView.getCheckRoomCallbackAccept().invalidCallbackNameAccept();
                break;
            case "errorCallbackNameAccept":
                this.answerView.getCheckRoomCallbackAccept().errorCallbackNameAccept();
                break;

            case "destroy":
                this.answerView.destroy();
                break;

            default:
                result.notImplemented();
        }

    }

    @Override
    public void dispose() {}
}
